//
//  YongHuPingJiaViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/9.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "YongHuPingJiaViewController.h"

@interface YongHuPingJiaViewController ()

@end

@implementation YongHuPingJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.tableview registerNib:[UINib nibWithNibName:@"PingJiaTableViewCell" bundle:nil] forCellReuseIdentifier:@"ping"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [QingQiuShuJu HuoQuPeiHuPingJia:@(self.carerId) HuiChuan:^(NSMutableArray *arr, BOOL isOK)
    {
        
        if (isOK) {
            
            if ([[arr valueForKey:@"status"] intValue]==200) {
                
                self.datas = [arr valueForKey:@"data"];
                self.tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

                [self.tableview reloadData];
                
            }
            else{
                
                
            }
        }
        else{
            
        }
        
    }];
    
}
#pragma mark---UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [[self.datas valueForKey:@"comments"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PingJiaTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"ping" forIndexPath:indexPath];
    
    id data = [self.datas valueForKey:@"comments"] [indexPath.row];
    
    cell.lable1.text = [NSString stringWithFormat:@"%@:",[data valueForKey:@"nickname"]==[NSNull null]?@"":[data valueForKey:@"nickname"]];
    
    cell.lable2.text = [self setTime : [data valueForKey:@"updated"]==[NSNull null]?(0):[[data valueForKey:@"updated"] longLongValue]];
    
    cell.lable3.text = [data valueForKey:@"content"]==[NSNull null]?@"":[data valueForKey:@"content"];
    
    NSString * avg= [data valueForKey:@"totalGrade"] == [NSNull null]?@"":[data valueForKey:@"totalGrade"];
    switch ([avg intValue]) {
        case 0:
        {
            cell.imageView1.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView2.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView3.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 1:
        {
            cell.imageView1.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView2.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView3.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 2:
        {
            cell.imageView1.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView2.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView3.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 3:
        {
            cell.imageView1.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView2.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView3.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView4.image = [UIImage imageNamed:@"wusewujiaoxing"];
            cell.imageView5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 4:
        {
            cell.imageView1.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView2.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView3.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView4.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView5.image = [UIImage imageNamed:@"wusewujiaoxing"];
        }
            break;
        case 5:
        {   cell.imageView1.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView2.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView3.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView4.image = [UIImage imageNamed:@"wujiaoxing"];
            cell.imageView5.image = [UIImage imageNamed:@"wujiaoxing"];
            
        }
            break;
            
        default:
            break;
    }
    

    
   
    
    return cell;
    
}
#pragma mark--UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  id data = [self.datas valueForKey:@"comments"] [indexPath.row];
    
      CGRect  rect = [[data valueForKey:@"content"]==[NSNull null]?@"":[data valueForKey:@"content"] boundingRectWithSize:CGSizeMake(kScreenW-32, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13] } context:nil];
    
    return 40 + rect.size.height+5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*v=[[UIView alloc]init];
    v.backgroundColor=CL_00cc85;
    UILabel*lable1=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, kScreenW, 34)];
    
    CGFloat  float1 = [[self.datas valueForKey:@"statComment"]valueForKey:@"avg"]==[NSNull null]?0:[[[self.datas valueForKey:@"statComment"]valueForKey:@"avg"] floatValue];
    
//    int x =float1/5;
    
    int x=(float1/5)*100;
    
    lable1.text=[NSString stringWithFormat:@"累计评价%ld （好评%d%%）",(long)self.numberPage,x];
    lable1.textColor=[UIColor whiteColor];
    [v addSubview:lable1];
    return v;
    
}
- (NSString *)setTime:(long long)lon{
    
    NSDate * date =[NSDate dateWithTimeIntervalSince1970:lon/1000];
    
    NSDateFormatter * dataformatter = [[NSDateFormatter alloc]init];
    
    [dataformatter setDateFormat:@"yyyy:MM:dd  HH:mm"];
    
    NSString * string = [dataformatter stringFromDate:date];
    
    return string;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
